package com.hegongda.servlet;

import com.hegongda.bean.Cart;
import com.hegongda.bean.CartItem;
import com.hegongda.service.CartService;
import com.hegongda.utils.BeanFactory;
import net.sf.json.JSONObject;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Collection;
import java.util.HashMap;
import java.util.Map;


@WebServlet(value = "/cartServlet")
public class CartServlet extends BaseServlet {
    public void editCart(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String pid = request.getParameter("pid");
        String count = request.getParameter("count");
        CartService cs = BeanFactory.newInstance(CartService.class);
        Cart cart = getCart(request);
        Cart rcarts = (Cart)request.getSession().getAttribute("carts");
        cs.editCart(pid,rcarts,cart,count);
        Map<String,Double> map = new HashMap<String,Double>();
        map.put("total",cart.getTotal());
        map.put("total1",rcarts.getTotal());
        map.put("subtotal",cart.getItemMap().get(pid).getSubTotal());
        response.getWriter().print(JSONObject.fromObject(new Result(Result.SUCCESS,"",map)));
    }

    public void selectAll(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        Cart cart = getCart(request);
        Cart rcarts = (Cart)request.getSession().getAttribute("carts");
        rcarts.setTotal(cart.getTotal());
        rcarts.setItemMap((HashMap<String,CartItem>)cart.getItemMap().clone());
        Map<String,Double> map = new HashMap<String,Double>();
        map.put("total",cart.getTotal());
        map.put("total1",rcarts.getTotal());
        response.getWriter().print(JSONObject.fromObject(new Result(Result.SUCCESS,"",map)));
    }


    public void addCart(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String pid = request.getParameter("pid");
        String count = request.getParameter("count");
        CartService cs = BeanFactory.newInstance(CartService.class);
        Cart cart = getCart(request);
        cs.addCart(pid,count,cart);
        response.sendRedirect(request.getContextPath()+"/view/cart/list.jsp");
    }

    public void addRCarts(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String pid = request.getParameter("pid");
        String style = request.getParameter("style");
        CartService cs = BeanFactory.newInstance(CartService.class);
        Cart cart = getCart(request);
        Cart rcarts = (Cart)request.getSession().getAttribute("carts");
        cs.addRCarts(pid,rcarts,cart,style);
        System.out.println(rcarts);
        Map<String,Double> map = new HashMap<String,Double>();
        map.put("total",cart.getTotal());
        map.put("total1",rcarts.getTotal());
        response.getWriter().print(JSONObject.fromObject(new Result(Result.SUCCESS,"",map)));
    }

    public void showCart(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.getSession().setAttribute("carts",new Cart());
        Cart cart = getCart(request);
        Result re = new Result(Result.SUCCESS,"购物车查询成功",cart);
        response.getWriter().print(JSONObject.fromObject(re));
    }
    private Cart getCart(HttpServletRequest request){
        //session域中,取出购物车对象
        Cart cart = (Cart) request.getSession().getAttribute("cart");
        if (cart==null){
            cart = new Cart();
            //购物车对象存储session
            request.getSession().setAttribute("cart",cart);
        }
        return  cart;
    }

    /**
     * 清空购物车的方法
     * session中取出购物车对象
     * 调用购物车对象方法clearItem清空
     */
    public void clearItem(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        CartService cs = BeanFactory.newInstance(CartService.class);
        Cart cart = getCart(request);
        request.getSession().setAttribute("carts",null);
        cs.clearItem(cart);
        Result re = new Result(Result.SUCCESS,"清空购物车成功");
        response.getWriter().print(JSONObject.fromObject(re));
    }
    /**
     *  移除购物项的方法
     *  接收请求传递的商品主键
     *  获取出购物车对象session
     *  调用购物车对象的方法 removeItem(主键)
     */
    public  void removeItem (HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //接收请求传递的商品主键
        String pid = request.getParameter("pid");
        //获取出购物车对象session
        Cart cart = getCart(request);
        CartService cs = BeanFactory.newInstance(CartService.class);
        cs.removeItem(pid,cart);
        Result re = new Result(Result.SUCCESS,"移除成功");
        response.getWriter().print(JSONObject.fromObject(re));
    }

}
